package org.youyee.game.managers
{
	import flash.display.DisplayObjectContainer;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	
	import org.youyee.game.displayArea.Area;
	import org.youyee.game.displayArea.DisplayMode;

	/**
	 * 区域深度管理器 
	 * @author youyee
	 * 
	 */	
	public class AreaDepthManager
	{
		private static var _sortResult:Array;
		private static var _visitedArea:Dictionary;
		private static var _dependency:Dictionary;
		private static var _depthIndex:int;
		
		/**
		 * 刷新区域的深度 
		 * @param areas 所有面积的对象
		 * @param container 容器
		 * @param mode 使用的视角模式
		 * 
		 */		
		public static function refreshDepth(areas:Vector.<Area>, container:DisplayObjectContainer, mode:int=0):void
		{
			if (mode == DisplayMode.MODE_45)
			{
				sortDepth(areas);
			}
			else
			{
				sortDepthNormal(areas);
			}
			
			var len:int = _sortResult.length;
			var a:Area;
			
			for (var i:int=0; i<len; ++i)
			{
				a = _sortResult[i] as Area;
				
				if (container.getChildIndex(a) != i)
				{
					container.setChildIndex(a, i);
				}
			}
			
			_dependency = null;
			_sortResult = null;
			_visitedArea = null;
			_depthIndex = 0;
		}
		
		private static function sortDepthNormal(areas:Vector.<Area>):void
		{
			toArray(areas);
			_sortResult.sortOn("y", Array.NUMERIC);
			toVector(areas);
		}
		
		private static function toArray(areas:Vector.<Area>):void
		{
			_sortResult = [];
			var len:int = areas.length;
			
			for (var i:int=0; i<len; ++i)
			{
				_sortResult[i] = areas[i];
			}
		}
		
		private static function toVector(areas:Vector.<Area>):void
		{
			var len:int = _sortResult.length;
			
			for (var i:int=0; i<len; ++i)
			{
				areas[i] = _sortResult[i] as Area;
			}
		}
			
		private static function sortDepth(areas:Vector.<Area>):void
		{
			_dependency = new Dictionary();
			
			var numAreas:int = areas.length;
			var behind:Array;
			var a:Area, b:Area;
			var i:int, j:int;
			var compareResult:int;
			
			for (i=0; i<numAreas; ++i)
			{
				behind = [];
				a = areas[i];
				
				for (j=0; j<numAreas; ++j)
				{
					if (i != j)
					{
						b = areas[j];
						compareResult = compareDepth(a, b);
						
						if (compareResult == 1)
						{
							behind.push(b);
						}
					}
				}
				
				_dependency[a] = behind;
			}
			
			_sortResult = [];
			_depthIndex = 0;
			_visitedArea = new Dictionary();
			
			for (i=0; i<numAreas; ++i)
			{
				a = areas[i];
				if (!_visitedArea[a])
				{
					placeAreaToIndex(a);
				}
			}
		}
		
		private static function placeAreaToIndex(area:Area):void
		{
			_visitedArea[area] = true;
			
			var behind:Array = _dependency[area] as Array;
			var len:int = behind.length;
			
			for (var i:int=0; i<len; ++i)
			{
				if (!_visitedArea[behind[i]])
				{
					placeAreaToIndex(behind[i]);
				}
			}
			
			_sortResult[_depthIndex] = area;
			++_depthIndex;
		}
		
		/**
		 * 对比两个区域的深度 
		 * @param areaA 区域1
		 * @param areaB 区域2
		 * @return 
		 * 
		 */		
		private static function compareDepth(areaA:Area, areaB:Area):int
		{
			//参照DepthManager里面的排序对比注释
			var aNode:Point = new Point(areaA.logicalX, areaA.logicalY);
			var bNode:Point = new Point(areaB.logicalX, areaB.logicalY);
			
			var aItemWidth:int = aNode.x + areaA.areaWidth;
			var bItemWidth:int = bNode.x + areaB.areaWidth;
			
			var aItemHeight:int = aNode.y + areaA.areaHeight;
			var bItemHeight:int = bNode.y + areaB.areaHeight;
			
			if (
				(aNode.x < bItemWidth && bNode.y < aNode.y && aItemWidth > bNode.x) || 
				(aNode.x > bNode.x && bItemHeight > aNode.y && aItemHeight > bNode.y) ||
				(aNode.x >= bItemWidth && aNode.y >= bItemHeight)
			)
			{
				return 1;
			}
			else if (
				(aNode.x < bNode.x && bItemHeight > aNode.y && bNode.y < aItemHeight) ||
				(aNode.y < bNode.y && bItemWidth > aNode.x && bNode.x < aItemWidth) ||
				(aItemWidth <= bNode.x && aItemHeight <= bNode.y)
			)
			{
				return -1;
			}
			
			return 0;
		}
	}
}